<?php
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Controller;

class AccountController extends CommonController
{
    public function _initialize()
    {
        $params = array('account_view');
        parent::_initialize($params);
        $this->assign("now_module" ,"recharge");
    }

    // 充值审核列表
    public function index()
    {
        $this->display();
    }
/*
    // 审核
    public function account_audit()
    {
        if(IS_POST){
            $remind = to_int(I("post.agree"));
            $account_id = to_int(I("post.account_id"));
            $remark = trim(I("post.remark"));

            $account = M("agent_account")->where("account_id = $account_id")->find();
            if(!$account){
                $this->error("参数有误" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $saveData['audit_status'] = $remind;
            $saveData['audit_remark'] = $remark;
            $saveData['audit_manager'] = session("manager_id");
            $saveData['audit_time'] = time();
            $rs = M("agent_account")->where("account_id = $account_id")->save($saveData);
            if($rs !== false){
                // 审核通过同意则需要添加预存金额
                if($remind == 1)
                {
                    // 更新预存金额
                    if(update_agent_account( $account_id)){
                        // 改变充值添加的状态
                        M("agent_account")->where("account_id = $account_id")->save(array("is_add" => 1));
                        $this->success("审核成功" ,U('Account/index') ,1);
                    }else{
                        M("agent_account")->where("account_id = $account_id")->save(array(
                            'audit_status' => 0,
                            'audit_remark' => '',
                            'audit_manager' => 0,
                            'audit_time' => 0,
                        ));
                        $this->error("更新预存款失败" ,$_SERVER['HTTP_REFERER'] ,1);
                    }
                }else{
                    $this->success("审核成功" ,U('Account/index') ,1);
                }
            }else{
                $this->error("审核失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $account_id = to_int(I("get.id"));
            $account = D("AccountView")->where("agent_account.account_id = $account_id")->find();
            if(!$account){
                $this->error("参数错误" ,U("Account/index") ,1);
            }
            $this->assign("account" ,$account);
            $this->display();
        }
    }
*/
    // 充值详情
    public function account_view()
    {
        $account_id = to_int(I("get.id"));
        $account = D("AccountView")->where("agent_account.account_id = $account_id")->find();
        if(!$account){
            $this->error("参数错误" ,U("Account/index") ,1);
        }
        $this->assign("account" ,$account);
        $this->display();
    }

    // 充值方式
    public function account_type()
    {
        $account_d = D("AccountView");

        $where = array();
        if(isset($_REQUEST['aName']))
        {
            $aName = trim(I('aName'));
            $status = to_int(I('astatus'));
            $ia = to_int(I('ia'));
            if($aName != ''){
                $where['agent.agent_name'] = array("like" ,"%$aName%");
            }
            if($status != ''){
                $where['agent_account.audit_status'] = array("eq" ,$status);
            }
            if($ia != ''){
                $where['agent_account.is_add'] = array("eq" ,$ia);
            }
        }
        $count = $account_d->where($where)->count();
        $page = set_page($count, C('PAGE_NUM'));
        $accountlist = $account_d->where($where)->limit($page->firstRow.','.$page->listRows)
            ->order('record_time ASC')->select();
        $show = $page->show();
        $this->assign('page',$show);
        $this->assign('accountlist',$accountlist);
        $this->assign('payType',$this->payType);
        $this->display();
    }

    // ajax 代理读取列表
    public function ajax_account_list()
    {
        $account_d = D("AccountView");
        $where = "agent.is_agent = 1 ";
        // 关键词搜索
        $aName = I('aName') ? trim(I('aName')) : '';
        if($aName) $where = "$where AND (agent.agent_name like '%$aName%')" ;
        (I('astatus') != '') && $where = "$where AND agent_account.audit_status = ". I('astatus');

        $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
        $count = $account_d->where($where)->count();
        $Page  = new AjaxPage($count, C('PAGE_NUM'));
        $accountlist = $account_d->where($where)->limit($Page->firstRow.','.$Page->listRows)->order($order_str)->select();
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('accountlist',$accountlist);
        $this->display();
    }
}